$(document).ready(function(){
    $("#submitID").button({
        icons: {
            primary: 'ui-icon-plusthick'
        }
    });
})

var req;

function ajaxFunction(){
    var url = "/salus/FileUploadServlet";

    if (window.XMLHttpRequest){
        req = new XMLHttpRequest();
        req.onreadystatechange = processStateChange;

        try{
            req.open("GET", url, true);

        } catch (e) {
            alert(e);
        }
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");

        if (req) {
            req.onreadystatechange = processStateChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processStateChange(){
    if (req.readyState == 4){
        if (req.status == 200){
            var xml = req.responseXML;
            var isNotFinished = xml.getElementsByTagName("finished")[0];
            var myBytesRead = xml.getElementsByTagName("bytes_read")[0];
            var myContentLength = xml.getElementsByTagName("content_length")[0];
            var myPercent = xml.getElementsByTagName("percent_complete")[0];
            if ((isNotFinished == null)){
                if(myPercent == null){
                    document.getElementById("initializing").style.visibility = "visible";
                    window.setTimeout("ajaxFunction();", 100);
                }
            } else {
                document.getElementById("initializing").style.visibility = "hidden";
                document.getElementById("progressBarTable").style.visibility = "visible";
                document.getElementById("percentCompleteTable").style.visibility = "visible";
                document.getElementById("bytesRead").style.visibility = "visible";

                myBytesRead = myBytesRead.firstChild.data;
                myContentLength = myContentLength.firstChild.data;

                if (myPercent != null) {
                    myPercent = myPercent.firstChild.data;

                    document.getElementById("progressBar").style.width = myPercent + "%";
                    document.getElementById("bytesRead").innerHTML = myBytesRead + " of " +
                    myContentLength + " bytes read";
                    document.getElementById("percentComplete").innerHTML = myPercent + "100%";
                    window.setTimeout("ajaxFunction();", 100);
                } else {
                    document.getElementById("bytesRead").style.visibility = "hidden";
                    document.getElementById("progressBar").style.width = "100%";
                    document.getElementById("percentComplete").innerHTML = "Archivo cargado!";
                    //document.getElementById("txtFile").value="";
                    listarDir();
                }
            }
        } else {
            alert(req.statusText);
        }
    }
}

function pop(pagina,ventana,parametros){
    window.open(pagina,ventana,parametros);
}

function listarDir(){
    var params = "dato="+document.getElementById("txtFile").value;
    jQuery.ajax({
        type: "POST",
        url: "/salus/ListarPlanos",
        data: params,
        dataType: 'text',
        success: function(msg){
            //jQuery("#listaPlanos").html(msg);
            if(parseInt(msg)>0){
                pop('/salus/ErroresCarga?file='+document.getElementById("txtFile").value,'Errores','top=0 ,left=0 ,width=100, height=100, scrollbars=yes, menubar=no, location=no, resizable=no');
            }
        //$('#ajax-loader').css('visibility','hidden');
        }
    });
}

function verErrores(file, error){
    
    var archivo = file.substr(0,file.length-4);
            
    switch (parseInt(error)) {
        case 1:
            pop('/salus/ErroresCarga?file='+archivo,'Errores','top=0 ,left=0 ,width=100, height=100, scrollbars=yes, menubar=no, location=no, resizable=no');
            break;
            
        case 2:
            alert('El archivo no esta en el formato indicado (ZIP)');
            break;
            
        case 3:
            alert('El tipo de archivo no es soportado');
            break;
        default:
            break;
    }

    
}